#! /usr/bin/python

from sys import argv
from os import path, remove, mkdir
from shutil import rmtree

global ACCOUNTS, AUDIT, GROUPS, FILES, TEST, COOKIE

def loginz(uname, pwd):
    global COOKIE

    if COOKIE.checkCK() ==0:
        # add to audit
        AUDIT.write(uname + ' logged in\n')
        # create global cookie
        COOKIE =cookie(uname)

    else:
        # add error to audit file
        AUDIT.write(uname + ' attempted to login on ' + 
                    COOKIE.retNM() + '\'s session\n')

def logoutz(uname):
    global COOKIE

    if COOKIE.checkCK() ==1 and COOKIE.retNM() ==uname:
        # add log out to audit file
        AUDIT.write(COOKIE.retNM() + ' successfully logged out\n')
        COOKIE.clearCK()
    else:
        AUDIT.write(uname + ' attempted to log out\n')

def addUsrz(uname, pwd):
    print 'some users'

    # if uname already user file
    #  output error msg

    # else add to user file

#def groupz(gname, uname):
#def createz(fname):
#def readz(fname):
#def writez(fname, txt):
#def executez(fname):
#def xcaclz(fname, opts):
#def end():

def fileprep(num):
    global ACCOUNTS, AUDIT, GROUPS, FILES
    
    if num ==0:
        if path.isdir('confidential'):
            rmtree('confidential')

        if path.isfile('confidential/accounts.txt'):
            remove('confidential/accounts.txt')
        if path.isfile('confidential/audit.txt'):
            remove('confidential/audit.txt')
        if path.isfile('confidential/groups.txt'):
            remove('confidential/groups.txt')
        if path.isfile('confidential/files.txt'):
            remove('confidential/files.txt')
    elif num ==1:
        mkdir('confidential')

        global ACCOUNTS
        ACCOUNTS = file('confidential/accounts.txt', 'w+')
        global AUDIT
        AUDIT = file('confidential/audit.txt', 'w')
        global GROUPS
        GROUPS = file('confidential/groups.txt', 'w')
        global FILES
        FILES = file('confidential/files.txt', 'w')
    elif num ==2:
        ACCOUNTS.close()
        AUDIT.close()
        GROUPS.close()
        FILES.close()

def auth():
    global ACCOUNTS, GROUPS, USERS

class cookie:
    def __init__(self, uname):
        if uname =='':
            self.check =0
        else:
            self.activeUsr =uname
            self.check =1
                    
    def checkCK(self):
        return self.check

    def retNM(self):
        return self.activeUsr

    def printNM(self):
        print self.activeUsr
        
    def clearCK(self):
        self.activeUsr =''
        self.check =0

def main():
    global COOKIE 
    COOKIE =cookie('')

    # clear output files
    fileprep(0)
    if len(argv) <1:
        print "could be a problem"

    elif len(argv) ==1 or len(argv) ==2:
        fileprep(1)

        if len(argv) ==2:
            try:
                TEST = open(argv[1]).readlines()
#               print TEST
            except IOError:
                print ':error: cannot open ', argv[1]
        elif len(argv) ==1:
            print ':error: no testcase'
            
            loginz('mordecia', 'boss')
            logoutz('frank')
            loginz('jim', 'shit')
            logoutz('mordecia')
            
            # must login
            loginz('peterk', 'hi')
            loginz('mkobe', 'bye')
            logoutz('peterk')
            logoutz('mkobe')
            
            loginz('mkobe', 'bye')
            logoutz('peterk') # error: shouldn't be able to logout
            logoutz('mkobe')
            
        # close files
        fileprep(2)
if __name__ == '__main__':
    main()
